<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <th:block th:include="include :: header('客流数据更新')"/>
    <th:block th:include="include :: select2-css"/>
    <th:block th:include="include :: datetimepicker-css"/>
</head>
<body class="gray-bg">
<div class="container-div">
    <div class="row">
        <div class="col-sm-12 ">
            <form id="do_query_form" class="form-horizontal">
                <div class="row">
                    <div class="col-sm-8">
                        <div class="form-group">
                            <label class="col-sm-2 control-label is-required">选择场所：</label>
                            <div class="col-sm-10">
                                <div class="input-group">

                                    <select id="deptIds2" class="form-control select2-multiple" required>
                                        <option th:each="dept : ${depts2}" th:value="${dept.deptId}"
                                                th:text="${dept.deptName}"></option>
                                    </select>

                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="col-sm-4">
                        <div class="form-group">
                            <label class="col-sm-4 control-label is-required">时间粒度：</label>
                            <div class="col-sm-8">
                                <div class="radio">
                                    <label>
                                        <input type="radio" checked="checked" value="hour" id="compTypeHour"
                                               name="compType">时</label>
                                    <label>
                                        <input type="radio" value="day" id="compTypeDay" name="compType">日</label>
                                    <label>
                                        <input type="radio" value="week" id="compTypeWeek" name="compType">周</label>
                                    <label>
                                        <input type="radio" value="month" id="compTypeMonth" name="compType">月</label>
                                    <label>
                                        <input type="radio" value="year" id="compTypeYear" name="compType">年</label>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="row">
                    <div class="col-sm-8">
                        <div class="form-group">
                            <label class="col-sm-2 control-label">选择场所：</label>
                            <div class="col-sm-10">
                                <div class="input-group">
                                    <select id="deptIds3" class="form-control select2-multiple" multiple>
                                        <option th:each="dept : ${depts3}" th:value="${dept.deptId}"
                                                th:text="${dept.deptName}"></option>
                                    </select>

                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="col-sm-4">
                        <div class="form-group">
                            <label class="col-sm-4 control-label is-required">日期范围：</label>
                            <div class="col-sm-6">
                                <div class="input-daterange input-group">
                                    <input type="text" class="input-sm form-control" id="startTime" name="startTime"
                                           readonly placeholder="yyyy-MM-dd" th:value="${startTime}"/>
                                    <span class="input-group-addon">到</span>
                                    <input type="text" class="input-sm form-control" id="endTime" name="endTime"
                                           readonly placeholder="yyyy-MM-dd" th:value="${endTime}"/>
                                </div>
                            </div>
                            <button type="button" class="btn btn-sm btn-primary" onclick="do_query()">确定</button>&nbsp;
                        </div>
                    </div>

                </div>
            </form>
        </div>


        <div class="col-sm-12">
            <div class="ibox float-e-margins">
                <div class="ibox-title">
                    <h5>数据详情</h5>

                </div>
                <div class="ibox-content">
                    <table
                            id="table"
                            data-toolbar="#toolbar"
                            data-search="true"
                            data-show-refresh="true"
                            data-show-toggle="true"
                            data-show-columns="true"
                            data-show-export="true"
                            data-show-print="true"
                            data-minimum-count-columns="2"
                            data-show-pagination-switch="true"
                            data-pagination="true"
                            data-id-field="id"
                            data-page-list="[10, 25, 50, 100, ALL]"
                            data-show-footer="false"
                            data-side-pagination="client">
                    </table>
                </div>
            </div>
        </div>

        <div class="col-sm-12">
            <div class="ibox float-e-margins">
                <div class="ibox-title">
                    <h5>手动数据更新 <small id="klzsTitle">注：输入正数值则是增加客流人数，输入负数值则是减少客流人数</small></h5>

                </div>
                <div class="ibox-content">
                    <form id="do_edit_form" role="form" class="form-inline">
                        <div class="form-group">
                            <input type="text" class="input-sm form-control" id="startTimeEdit"
                                   readonly placeholder="yyyy-mm-dd HH:mm" required th:value="${startTimeEdit}"/>
                        </div>
                        <div class="form-group">
                            <select id="deptId2" class="form-control select2-multiple" required>
                                <option th:each="dept : ${depts2}" th:value="${dept.deptId}"
                                        th:text="${dept.deptName}"></option>
                            </select>
                        </div>
                        <div class="form-group">
                            <select id="deptId3" class="form-control select2-multiple" required>
                                <option th:each="dept : ${depts3}" th:value="${dept.deptId}"
                                        th:text="${dept.deptName}"></option>
                            </select>
                        </div>
                        <div class="form-group">
                            <input type="text" id="passNum" class="input-sm form-control" placeholder="经过人数"/>
                        </div>
                        <div class="form-group">
                            <input type="text" id="inNum" class="input-sm form-control" placeholder="进入人数"/>
                        </div>
                        <div class="form-group">
                            <input type="text" id="outNum" class="input-sm form-control" placeholder="离开人数"/>
                        </div>
                        <div class="form-group">
                            <input type="text" id="repNum" class="input-sm form-control" placeholder="重复人数"/>
                        </div>
                        <button class="btn btn-sm btn-primary" type="button" onclick="updateStsDataDetailNum()">提交</button>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
<th:block th:include="include :: footer"/>
<th:block th:include="include :: echarts-js"/>
<th:block th:include="include :: bootstrap-table-export-js"/>
<th:block th:include="include :: bootstrap-table-print-js"/>
<th:block th:include="include :: select2-js"/>
<th:block th:include="include :: datetimepicker-js"/>
<script th:inline="javascript">

    var tableColumns = [{
        field: 'deptName',
        sortable: true,
        title: '位置'
    }, {
        field: 'startTime',
        sortable: true,
        title: '时间'
    }, {
        field: 'passNum',
        title: '经过',
        sortable: true
    },
        {
            field: 'inNum',
            title: '进入',
            sortable: true
        },
        {
            field: 'outNum',
            title: '离开',
            sortable: true
        },
        {
            field: 'repNum',
            title: '重复',
            sortable: true
        },
        {
            field: 'manNum',
            title: '男性',
            sortable: true
        },
        {
            field: 'womanNum',
            title: '女性',
            sortable: true
        },
        {
            field: 'unknownSexNum',
            title: '未知性别',
            sortable: true
        },
        {
            field: 'childNum',
            title: '儿童',
            sortable: true
        },
        {
            field: 'juvenileNum',
            title: '少年',
            sortable: true
        },
        {
            field: 'teenagerNum',
            title: '青少年',
            sortable: true
        },
        {
            field: 'youthNum',
            title: '青年',
            sortable: true
        },
        {
            field: 'adultNum',
            title: '壮年',
            sortable: true
        },
        {
            field: 'middleNum',
            title: '中年',
            sortable: true
        },
        {
            field: 'elderlyNum',
            title: '中老年',
            sortable: true
        },
        {
            field: 'seniorNum',
            title: '老年',
            sortable: true
        },
        {
            field: 'unknownAgeNum',
            title: '未知年龄',
            sortable: true
        }]
    $(function () {

        $("#startTime").datetimepicker({
            format: 'yyyy-mm-dd',
            minView: "month",
            todayBtn: true,
            autoclose: true,
            endDate: new Date(),
        }).on('changeDate', function (event) {
            event.preventDefault();
            event.stopPropagation();
            var startTime = event.date;
            $('#endTime').datetimepicker('setStartDate', startTime);
        });

        $("#endTime").datetimepicker({
            format: 'yyyy-mm-dd',
            minView: "month",
            todayBtn: true,
            autoclose: true,
            endDate: new Date(),
        }).on('changeDate', function (event) {
            event.preventDefault();
            event.stopPropagation();
            var endTime = event.date;
            $("#startTime").datetimepicker('setEndDate', endTime);
        });

        $("#startTimeEdit").datetimepicker({
            format: 'yyyy-mm-dd HH:mm',
            minView: 0,
            todayBtn: true,
            autoclose: true,
            endDate: new Date(),
            defaultDate: new Date()
        }).on('changeDate', function (event) {

        });

        $("#deptIds2").on("change", function(e) {
            $("#deptIds3").html("");
            var deptIds2 = $.form.selectSelects("deptIds2");
            $.ajax({
                cache: true,
                type: "GET",
                url: ctx + "system/dept/queryByParentId",
                data: {
                    "parentId": deptIds2,
                },
                async: false,
                error: function (request) {
                    $.modal.alertError("系统错误");
                },
                success: function (data) {
                    for (var i = 0; i < data.length; i++) {
                        var it = data[i];
                        $('#deptIds3').append($('<option>', {
                            value: it.deptId,
                            text: it.deptName
                        }));

                    }
                }
            });
        });


        $("#deptId2").on("change", function(e) {
            $("#deptId3").html("");
            var deptId2 = $.form.selectSelects("deptId2");
            $.ajax({
                cache: true,
                type: "GET",
                url: ctx + "system/dept/queryByParentId",
                data: {
                    "parentId": deptId2,
                },
                async: false,
                error: function (request) {
                    $.modal.alertError("系统错误");
                },
                success: function (data) {
                    for (var i = 0; i < data.length; i++) {
                        var it = data[i];
                        $('#deptId3').append($('<option>', {
                            value: it.deptId,
                            text: it.deptName
                        }));

                    }
                }
            });
        });

        var options = {
            sortName: "startTime",
            modalName: "数据详情",
            columns: tableColumns
        };
        for (var i = 2; i < tableColumns.length; i++) {
            var it = tableColumns[i];
            $('#columns').append($('<option>', {
                value: it.field,
                text: it.title
            }));
        }
        $('#table').bootstrapTable(options);
    });


    function do_query() {
        if (!$("#do_query_form").validate().form()) {
            return
        }

        var deptIds2 = $.form.selectSelects("deptIds2");
        var deptIds3 = $.form.selectSelects("deptIds3");
        var startTime = $("input[name='startTime']").val();
        var endTime = $("input[name='endTime']").val();
        var compType = $("input[name='compType']:checked").val();
        var dateFormat = "%Y-%m-%d";
        if (compType === "hour") {
            dateFormat = "%Y-%m-%d %H:00:00";
            $("input[name='endTime']").val(startTime);
            endTime = startTime;
        } else if (compType === "day") {
            dateFormat = "%Y-%m-%d";
        } else if (compType === "week") {
            dateFormat = "%Y-01-01 00:%u:01";
        } else if (compType === "month") {
            dateFormat = "%Y-%m-01";
        } else if (compType === "year") {
            dateFormat = "%Y-01-01";
        }
        var deptIdAttr = "dept_id2";
        var deptNameAttr = "dept_name2";
        var deptIds = deptIds2;
        if(deptIds3){
            deptIdAttr = "dept_id3";
            deptNameAttr = "dept_name3";
            deptIds = deptIds3;
        }
        $.ajax({
            cache: true,
            type: "POST",
            url: ctx + "sts/data/detail/deptStatistic",
            data: {
                "deptIdAttr": deptIdAttr,
                "deptNameAttr": deptNameAttr,
                "deptIds": deptIds,
                "deptIds2": deptIds2,
                "deptIds3": deptIds3,
                "startTime": startTime,
                "endTime": endTime,
                "dateFormat": dateFormat
            },
            async: false,
            error: function (request) {
                $.modal.alertError("系统错误");
            },
            success: function (data) {
                for (var i = 0; i < data.length; i++) {
                    var it = data[i];

                    var compType = $("input[name='compType']:checked").val();
                    if (compType === "hour") {
                        it.startTime = it.startTime.substring(0, 13);
                    } else if (compType === "day") {
                        it.startTime = it.startTime.substring(0, 10);
                    } else if (compType === "week") {
                        it.startTime = it.startTime.substring(0, 4) + '-' + it.startTime.substring(14, 16) + "周";
                    } else if (compType === "month") {
                        it.startTime = it.startTime.substring(0, 7);
                    } else if (compType === "year") {
                        it.startTime = it.startTime.substring(0, 4);
                    }

                    if(it.deptName3){
                        it.deptName = it.deptName3;
                    }else{
                        it.deptName = it.deptName2;
                    }

                }
                $('#table').bootstrapTable('load', data);
            }
        });
    }

    function updateStsDataDetailNum(){
        if (!$("#do_edit_form").validate().form()) {
            return
        }
        var deptId2 = $.form.selectSelects("deptId2");
        var deptId3 = $.form.selectSelects("deptId3");
        var startTime = $("#startTimeEdit").val();
        var passNum = $("#passNum").val();
        var inNum = $("#inNum").val();
        var outNum = $("#outNum").val();
        var repNum = $("#repNum").val();
        $.ajax({
            cache: true,
            type: "POST",
            url: ctx + "sts/data/detail/editNum",
            data: {
                "deptId2": Number(deptId2),
                "deptId3": Number(deptId3),
                "startTime": startTime,
                "passNum": Number(passNum),
                "inNum": Number(inNum),
                "outNum": Number(outNum),
                "repNum": Number(repNum),
            },
            async: false,
            error: function (request) {
                $.modal.alertError("系统错误");
            },
            success: function (data) {
                if(data.code === 0){
                    $.modal.msgSuccess("操作成功");
                }else{
                    $.modal.msgError(data.msg);
                }
            }
        });
    }
</script>
</body>
</html>